package com.example.rentalcar.controller;

import com.example.rentalcar.entity.Users;
import com.example.rentalcar.service.impl.OssServiceImpl;
import com.example.rentalcar.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/users")
public class UserController {

    @Autowired
    UserServiceImpl userService;
    @Autowired
    OssServiceImpl ossService;


    @PostMapping("/login")
    public ResponseEntity<Map<String, String>> login(@RequestBody Map<String, String> loginRequest) {
        String username = loginRequest.get("username");
        String password = loginRequest.get("password");

        // 调用你的登录和生成令牌的方法
        String token = userService.loginAndGenerateToken(username, password);

        if (token != null) {
            Map<String, String> data = new HashMap<>();
            data.put("token", token);
            System.out.println("success");
            return ResponseEntity.ok(data);

        } else {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
        }
    }


    @GetMapping("/{userId}")
    public Users getUserById(@PathVariable Long userId) {
        return userService.findById(userId);
    }

    @GetMapping("/all")
    public List<Users> getAllUsers(@RequestParam int start, @RequestParam int limit) {
        return userService.findAll(start, limit);
    }

    @GetMapping("/by-username")
    public Users getUserByUsername(@RequestParam String userName) {
        return userService.findByUsername(userName);
    }

    @GetMapping("/by-phone")
    public Users getUserByPhone(@RequestParam String userPhone) {
        return userService.findByUserPhone(userPhone);
    }


    @PutMapping("/update")
    public void updateUser(@RequestBody Users user) {
        userService.update(user);
    }

    @DeleteMapping("/delete/{userId}")
    public void deleteUserById(@PathVariable Long userId) {
        userService.deleteById(userId);
    }

    @PostMapping("/save")
    public ResponseEntity<String> saveUser(
            @RequestParam("userName") String userName,
            @RequestParam("userPhone") String userPhone,
            @RequestParam("userSex") String userSex,
            @RequestParam("userPassword") String userPassword,
            @RequestPart("userIdcard1") MultipartFile userIdcard1,
            @RequestPart("userIdcard2") MultipartFile userIdcard2,
            @RequestPart("userDriveCard1") MultipartFile userDriveCard1,
            @RequestPart("userDriveCard2") MultipartFile userDriveCard2) {

        try {
            // 创建 User1 对象，设置字段值
            Users user = new Users();
            user.setUserName(userName);
            user.setUserPhone(userPhone);
            user.setUserSex(userSex);
            user.setUserPassword(userPassword);
            user.setUserStu(0);

            // 上传图片并获取图片URL
            String userIdcardImage1Url = ossService.uploadImageToOSS(userIdcard1);
            String userIdcardImage2Url = ossService.uploadImageToOSS(userIdcard2);
            String userDrivecardImage1Url = ossService.uploadImageToOSS(userDriveCard1);
            String userDrivecardImage2Url = ossService.uploadImageToOSS(userDriveCard2);

            // 设置用户对象中的图片URL属性
            user.setUserDriveCard1(userDrivecardImage1Url);
            user.setUserIdcard1(userIdcardImage1Url);
            user.setUserDriveCard2(userDrivecardImage2Url);
            user.setUserIdcard2(userIdcardImage2Url);

            // 调用保存用户的服务方法
            userService.save(user);

            // 返回成功响应
            return ResponseEntity.ok("User saved successfully!");

        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to save user");
        }
    }

    @GetMapping("/updateState1/{userId}")
    public void UpdateState1ById(@PathVariable Long userId) {
        Users user = userService.findById(userId);
        if (user != null) {
            user.setUserStu(1);
            userService.update(user);
        }


    }

    @GetMapping("/updateState0/{userId}")
    public void UpdateState0ById(@PathVariable Long userId) {
        Users user = userService.findById(userId);
        if (user != null) {
            user.setUserStu(0);
            userService.update(user);
        }


    }

    @GetMapping("/updateState2/{userId}")
    public void UpdateState2ById(@PathVariable Long userId) {
        Users user = userService.findById(userId);
        if (user != null) {
            user.setUserStu(2);
            userService.update(user);
        }

    }


}
